package com.yue.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yue.entity.PartJobCategory;
import com.yue.entity.vo.PartJobVO;
import com.yue.entity.PartJob;
import com.yue.entity.User;
import com.yue.excetion.ExceptionEnum;
import com.yue.excetion.PartJobException;
import com.yue.mapper.PartJobMapper;
import com.yue.service.PartJobCategoryService;
import com.yue.service.PartJobService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yue.service.UserService;
import com.yue.utils.LocalDateTimeUtil;
import com.yue.utils.UUIDUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 兼职信息表 服务实现类
 * </p>
 *
 * @author YG
 * @since 2022-03-23
 */
@Service
public class PartJobServiceImpl extends ServiceImpl<PartJobMapper, PartJob> implements PartJobService {

    @Autowired
    PartJobMapper partJobMapper;

    @Autowired
    private PartJobCategoryService partJobCategoryService;
    @Autowired
    UserService userService;

    /**
     * 保存或者修改兼职
     * @param partJob
     */
    @Override
    public void saveOrEditPartJob(PartJob partJob) {
        //获取当前登录的用户信息
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        User user = userService.getUserByUsername(username);
        //判断是新增还是修改
        if(StrUtil.isBlank(partJob.getPartJobId())){
            partJob.setUserId(user.getUserId());
            partJob.setPartJobId(UUIDUtil.getUUID());
            partJob.setDelFlag(false);
            partJob.setCommentCount(0);
            partJob.setCreateTime(LocalDateTimeUtil.getCurrentLocalDateTime());
            partJob.setStatus(0);
        }else{
            //确保当前修改这篇兼职的人是创建者
            if(!user.getUserId().equals(partJob.getUserId())){
                throw new PartJobException(ExceptionEnum.PASSWORD_NOT_EXISTED);
            }
        }
        try{
            this.saveOrUpdate(partJob);
        }catch (Exception e){
            e.printStackTrace();
            throw new PartJobException(ExceptionEnum.PART_JOB_EDIT_ERROR);
        }
    }

    /**
     * 根据关键词搜索兼职（title和description对应即可）
     * @param word
     * @return
     */
    @Override
    public Page<PartJobVO> searchPartJobByWord(Page<PartJobVO> page,String word) {
        return this.partJobMapper.searchPartJobByWord(page,word);
    }

    @Override
    public Page<PartJobVO> searchPartJobVO(Page<PartJobVO> page) {
        return this.partJobMapper.searchPartJob(page);
    }

    @Override
    public Page<PartJobVO> searchPartJobByCategory(Page<PartJobVO> page, String categoryId) {

        // 获取分类信息
        PartJobCategory one = this.partJobCategoryService.getOne(new LambdaQueryWrapper<PartJobCategory>()
                .eq(PartJobCategory::getCategoryId, categoryId));

        // 获取分类下所有的兼职
        Page<PartJob> partJobVOPage = new Page<>();

        Page<PartJob> front = this.page(partJobVOPage, new LambdaQueryWrapper<PartJob>().eq(PartJob::getCategoryId, one.getCategoryId()));

        List<PartJob> records = front.getRecords();

        List<PartJobVO> collect = records.stream().map(item -> {

            PartJobVO partJobVO = new PartJobVO();
            BeanUtils.copyProperties(item, partJobVO);
            partJobVO.setCategoryName(one.getCategoryName());

            return partJobVO;
        }).collect(Collectors.toList());

        Page<PartJobVO> result = new Page<PartJobVO>();
        result.setCurrent(front.getCurrent());
        result.setSize(front.getSize());
        result.setTotal(front.getTotal());
        result.setRecords(collect);
        result.setPages(front.getPages());




        return result;
    }

    /**
     * 将兼职的评论id加一
     * @param partJobId
     */
    @Override
    public void addPartJobCommentCount(String partJobId) {
        PartJob partJob = this.partJobMapper.selectById(partJobId);
        if(ObjectUtil.isEmpty(partJob.getCommentCount())){
            partJob.setCommentCount(0);
        }
        partJob.setCommentCount(partJob.getCommentCount()+1);
        this.partJobMapper.updateById(partJob);
    }

    /**
     * 设置兼职状态为-1
     * @param partJobId
     */
    @Override
    public Integer setStatus(String partJobId, String status) {
        if ("-1".equals(status)) {
            status = "1";
        } else if ("1".equals(status)) {
            status = "-1";
        }
        Integer res = partJobMapper.setStatus(partJobId, status);
        return res;
    }

    /**
     * 获取待审核的兼职
     * @param page
     * @return
     */
    @Override
    public IPage getCheckJob(Page page, Integer jobType , Integer currentPage) {
        return partJobMapper.getCheckJob(page, jobType, currentPage);
    }

}
